package com.adwin.addcoin.ui.webview

import android.graphics.Bitmap
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.RelativeLayout
import android.widget.RelativeLayout.LayoutParams
import androidx.lifecycle.lifecycleScope
import com.adwin.addcoin.BuildConfig
import com.adwin.addcoin.base.BaseActivity
import com.adwin.addcoin.base.BaseWebView
import com.adwin.addcoin.databinding.ActivityWebviewBinding
import com.adwin.addcoin.repository.h5.H5SendAppCommunication
import com.adwin.addcoin.ui.main.MainViewModel
import com.adwin.addcoin.utils.FlowKeyUtils
import com.adwin.addcoin.utils.log
import com.adwin.addcoin.utils.startBrowser
import com.cashbird.app.infra.utils.FlowBus

class WebViewActivity :BaseActivity<ActivityWebviewBinding,MainViewModel>(
    MainViewModel::class.java
) {
    private var webview:BaseWebView? = null
//    private val shopUrl = "https://www.baidu.com"
//
    private var shopUrl = ""
    override fun inflater(): ActivityWebviewBinding {
        val binding = ActivityWebviewBinding.inflate(LayoutInflater.from(this))
        setContentView(binding.root)
        return binding
    }

    override fun initActivity() {
        fullScreen(this)
        shopUrl = intent.getStringExtra("url")?:""
        webview = BaseWebView(this.applicationContext)
        webview?.clearCache(true)
        webview?.loadUrl(shopUrl)

        webview?.initWeb()
        val layoutParams = LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)
        webview?.layoutParams = layoutParams
        binding.rlWebLayout.addView(webview)
        webview?.webChromeClient = object : WebChromeClient(){
            override fun onReceivedTitle(view: WebView?, title: String?) {
                super.onReceivedTitle(view, title)
                if (!TextUtils.isEmpty(title)){
                    binding.tvWebTitle.text = title
                    binding.tvWebTitle.visibility = View.VISIBLE
                    binding.ivWebReturn.visibility = View.VISIBLE
                    binding.viewTopDrawable.visibility = View.VISIBLE
                }else{
                    binding.tvWebTitle.visibility = View.GONE
                    binding.ivWebReturn.visibility = View.GONE
                    binding.viewTopDrawable.visibility = View.GONE
                }
            }


        }
        webview?.webViewClient = object : WebViewClient(){



            override fun shouldOverrideUrlLoading(
                view: WebView?,
                request: WebResourceRequest?
            ): Boolean {
                if (request != null&& request.url != null){
                    val scheme = request.url.scheme
                    "dddd  ${scheme}".log()
                    return if (scheme != "http"&&scheme != "https"){
                        val url = request.url.toString()
                        if (!startBrowser(url)){
                            if (url.indexOf("whatsapp://") != -1){
                                startBrowser("https://play.google.com/store/apps/details?id=com.whatsapp")
                            }else if (url.indexOf("tg:resolv") != -1){
                                startBrowser("https://play.google.com/store/apps/details?id=org.telegram.messenger")
                            }
                        }
                        true
                    }else{
                        false
                    }
                }
                return super.shouldOverrideUrlLoading(view, request)
            }


        }
        binding.ivWebReturn.setOnClickListener {
            finish()
        }
        FlowBus.with<String>(FlowKeyUtils.finishActivity).register(lifecycle){
            finish()
        }

    }

    override fun onPause() {
        super.onPause()
        webview?.pauseTimers()
    }

    override fun onResume() {
        super.onResume()
        webview?.resumeTimers()
    }

    override fun onDestroy() {
        if (webview!=null){
            webview?.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            webview?.clearHistory()
            (webview?.parent as RelativeLayout).removeAllViews()
            webview?.destroy()
            webview = null
        }
        super.onDestroy()

    }
}